unit UDevIOEdit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UDevIOAddEditBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinOffice2016Colorful, Vcl.Menus, Data.DB, MemDS, DBAccess,
  Uni, System.ImageList, Vcl.ImgList, cxImageList, Vcl.StdCtrls, cxButtons,
  Vcl.ExtCtrls, cxSpinEdit, cxDropDownEdit, cxMaskEdit, cxTextEdit, cxLabel;

type
  TfrmDevIOEdit = class(TfrmDevIOAddEditBase)
    procedure FormShow(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDevIOEdit: TfrmDevIOEdit;

implementation

uses
  UCommonFunSqlite, UCommonConst;

{$R *.dfm}

procedure TfrmDevIOEdit.btnSaveClick(Sender: TObject);
var
  sSql, sIOName, sIP: string;
  iPort, iCtrlID: Integer;
begin
  inherited;

  sIOName := Trim(edtName.Text);
  if sIOName.IsEmpty then
  begin
    MsgBoxDefault(RESDEVNAMEEMPTY);
    edtName.SetFocus;
    Abort;
  end;

  sSql := 'select IOName from DevIOData where IOName=' + QuotedStr(sIOName) + ' and IOID<>' + QuotedStr(IntToStr(FSelectID));
  if getSingleValueStr(sSql) <> '' then
  begin
    MsgBoxDefault(RESDEVNAMEEXIST);
    edtName.SetFocus;
    Abort;
  end;

  sIP := Trim(edtIP.Text);
  if sIP.IsEmpty then
  begin
    MsgBoxDefault(RESDEVIPEMPTY);
    edtIP.SetFocus;
    Abort;
  end;

  sSql := 'select IOIP from DevIOData where IOIP=' + QuotedStr(sIP) + ' and IOID<>' + QuotedStr(IntToStr(FSelectID));
  if getSingleValueStr(sSql) <> '' then
  begin
    MsgBoxDefault(RESDEVIPEMPTY);
    edtIP.SetFocus;
    Abort;
  end;


  iPort := edtPort.Value;

  if comDevCtrl.ItemIndex = -1 then
  begin
    MsgBoxDefault(RESDEVCTRLNOSELECTED);
    comDevCtrl.SetFocus;
    Abort;
  end;

  iCtrlID := Integer(comDevCtrl.Properties.Items.Objects[comDevCtrl.ItemIndex]);

  sSql := 'update DevIOData set IOName=' + QuotedStr(sIOName)
        + ',IOIP=' + QuotedStr(sIP)
        + ',IOPort=' + QuotedStr(IntToStr(iPort))
        + ',DevCtrlID=' + QuotedStr(IntToStr(iCtrlID))
        + ' where IOID=' + QuotedStr(IntToStr(FSelectID));

  if ExecSql(sSql) then
  begin
    SendMessage(FParentHandle, WM_REFRESHDATA, 0, 0);

    MsgBoxDefault(RESSAVESUCCESS);
  end
  else
    MsgBoxDefault(RESSAVEFAIL);

end;

procedure TfrmDevIOEdit.FormShow(Sender: TObject);
var
  sSql: string;
  iCtrlID: Integer;
begin
  inherited;

  sSql := 'select * from DevIOData where IOID=''' + IntToStr(FSelectID) + '''';

  with qryTmp do
  begin
    close;
    SQL.Text := sSql;
    Open;

    edtName.Text := Trim(FieldByName('IOName').AsString);
    edtIP.Text := Trim(FieldByName('IOIP').AsString);
    edtPort.Value := FieldByName('IOPort').AsInteger;

    iCtrlID := FieldByName('DevCtrlID').AsInteger;
    LocationCom(comDevCtrl, iCtrlID);
  end;
end;

end.
